package com.fivestar.pmanager.dao.mapper;

import java.util.List;

import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import com.fivestar.pmanager.core.mapper.DBActionMapper;
import com.fivestar.pmanager.core.mapper.PaginatingMapper;
import com.fivestar.pmanager.model.FsUserBean;
import com.fivestar.pmanager.model.SysRoleBean;
import com.fivestar.pmanager.model.SysRoleUserBean;


/**
 * 角色用户关系表
 * SysRoleUserMapper
 * @author 
 *
 */
@Repository("sysRoleUserMapper")
public interface SysRoleUserMapper extends DBActionMapper<SysRoleUserBean>,
        PaginatingMapper<SysRoleUserBean>
{
    @Results({
            @Result(property = "code", column = "code")
    })
    @Select("select sr.`code` from sys_role sr " +
            "join sys_role_user sru on sru.role_id = sr.row_id " +
            "where sru.user_id=#{userId} ")
    List<SysRoleBean> findUserRole(@Param("userId")int userId);
    
    @Results({
        @Result(property = "rowId", column = "row_id"),
        @Result(property = "nickname", column = "nickname")
	})
	@Select("select mu.row_id,mu.nickname from md_user mu " +
	        "join sys_role_user sru on sru.user_id = mu.row_id " +
	        "where sru.role_id=#{roleId}")
	List<FsUserBean> findRoleUser(@Param("roleId")int roleId);
    
    @Results({
        @Result(property = "rowId", column = "row_id")
	})
	@Select("select mu.row_id,mu.nickname from md_user mu " +
	        "join sys_role_user sru on sru.user_id = mu.row_id " +
	        "where sru.role_id=#{roleId}")
	List<Integer> findRoleUserIds(@Param("roleId")int roleId);
    
    int deleteBySelective(SysRoleUserBean sysRoleUserBean);

    @ResultType(String.class)
    @Select("select CONVERT(group_concat(ru.role_id),char) from sys_role_user ru where ru.user_id=#{userId}")
    String getRoles(@Param("userId")int userId);
}
